﻿@model Umbraco.Cms.Web.Model.BackOffice.Editors.UserGroupEditorModel
@using Umbraco.Framework
@using Umbraco.Framework.Persistence.Model.Constants.Schemas
@{
    Layout = "../Shared/_TabbedEditorLayout.cshtml";
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();

    //Change the action to post to depending on if the entity is new or not
    ViewBag.Action = !Model.Id.IsNullValueOrEmpty() ? "Edit" : "Create";
    //if the model is not null, then DONT post any Id to the Create method, otherwise post
    //the Id as per normal
    ViewBag.UrlParams = !Model.Id.IsNullValueOrEmpty()
        ? (object)new { id = Model.Id }
        : (object)new { id = "" };
}
@section Head {
    @{
        Html
            .RequiresJs("Umbraco.UI/PermissionsGrid.js", "Scripts")
            .RequiresJs("Umbraco.Editors/UserGroupEditor.js", "Scripts");
    }
    <script type="text/javascript">
        (function ($) {
           $(document).ready(function () {                
                Umbraco.Editors.UserGroupEditor.getInstance().init({
                    activeTabIndex: @Model.ActiveTabIndex ,
                    activeTabIndexField: $("#@Html.IdFor(x => Model.ActiveTabIndex)"),
                    permissions: @Html.ToJsonString(@Model.Permissions),
                    isReadOnly: @((Model.Name == "Administrator").ToString().ToLower())
                }); 
            });
        })(jQuery);
    </script>
}
@section Tabs {
    <li><a href="#macroPropertiesTab"><span>@(string.IsNullOrEmpty(Model.Name) ? "New User Group" : Model.Name)</span></a></li>
    <li><a href="#umbracoPropertiesTab"><span>Umbraco Properties</span></a></li>
}
@using (Html.BeginForm((string)ViewBag.Action, new Guid(CorePluginConstants.UserGroupEditorControllerId), (object)ViewBag.UrlParams, new { enctype = "multipart/form-data" }))
{
    @Html.UmbValidationSummary("Please correct the errors and try again.")
    
    @Html.UmbEditorBar();
 
    @Html.HiddenFor(x => Model.ActiveTabIndex)

    <div id="editorContent" class="has-editor-bar scrolling-content padded-content">
        
        @if (Model.Id.IsNullValueOrEmpty())
        {
            <p class="notice">@("UserGroup.UnSaved.Message".Localize())</p>
        }
        
        <p class="notice">@("UserGroup.Inherit.Message".Localize())</p>
        
        @*Render User properties tab*@
        <div id="macroPropertiesTab">
            <div class="property-pane clearfix">
                @if(Model.Name == "Administrator" || Model.Name == "Anonymous")
                {
                    @Html.UmbDisplayFor(x => Model.Name)
                } 
                else
                {
                    @Html.UmbEditorFor(x => Model.Name)
                }
            </div>
            <div class="property-pane clearfix">
                <div class="property-editor clearfix">
                    <div class="property-editor-label">Permissions</div>
                    <div class="property-editor-control permissions-grids-container">
                        <div id="@Model.Id.GetHtmlId()" class="permissions-grid-container" style="max-width: 500px;"></div>
                    </div>
                </div>
            </div>
            @{
                var usersProperty = Model.Properties.SingleOrDefault(x => x.Alias == UserGroupSchema.UsersAlias);
                if (usersProperty != null)
                {
                    @Html.EditorFor(editor => usersProperty, "ContentProperty", usersProperty.Id.GetHtmlId())
                }
            }
        </div>
        @*Render Umbraco properties tab*@
        <div id="umbracoPropertiesTab">
            <div class="property-pane clearfix">
                @Html.UmbDisplayId(f => Model.Id)
                @Html.UmbDisplayFor(m => Model.DocumentTypeName)
                @Html.UmbDisplayFor(f => Model.UtcCreated)
                @Html.UmbDisplayFor(f => Model.UtcModified)
                @Html.UmbDisplayFor(f => Model.CreatedBy)
                @Html.UmbDisplayFor(f => Model.UpdatedBy)
            </div>
        </div>
    </div>
}
